<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
            <el-form-item label="日期查询">
                <el-date-picker v-model="value" type="daterange" :clearable="false" value-format="yyyy-MM-dd"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <!-- <el-button type="success" icon="el-icon-refresh" size="mini" @click="resetQuery">下发信号</el-button> -->
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" plain icon="el-icon-download" size="mini"
                            @click="handleExport">导出</el-button>
                    </el-col>
                </el-row>
        <el-table v-loading="loading" :data="basicauthorList" @selection-change="handleSelectionChange"
            style="margin-top: 20px;">

            <el-table-column label="设备名称" align="center" prop="facility_name" />
            <el-table-column label="产品编号" align="center" prop="sequence" />
            <el-table-column label="操作员" align="center" prop="option" />
            <el-table-column label="采集数据" prop="db_message_resp" align="center">
                <template slot-scope="scope">
                    <div v-if="scope.row.db_message_resp.length > 2 && scope.row.offShow">
                        <el-row>
                            <el-col :span="12">{{ scope.row.db_message_resp[0].key }}</el-col>
                            <el-col :span="12">{{ scope.row.db_message_resp[0].value }}</el-col>
                            <el-col :span="12">{{ scope.row.db_message_resp[1].key }}</el-col>
                            <el-col :span="12">{{ scope.row.db_message_resp[1].value }}</el-col>
                            <span style="color: #409eff;font-size: 12px;cursor: pointer;"
                                @click="openTable(scope, $event)">展开</span>
                        </el-row>
                    </div>
                    <div v-else>
                        <el-row v-for="(item, index) in scope.row.db_message_resp" :key="index">
                            <el-col :span="12">{{ item.key }}</el-col>
                            <el-col :span="12">{{ item.value }}</el-col>
                        </el-row>
                        <span style="color: #409eff;font-size: 12px;cursor: pointer;" @click="openTable(scope, $event)"
                            v-if="scope.row.db_message_resp.length > 2">收起</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="MES判断是否合格" align="center" prop="status_mes">
                <template slot-scope="scope">
                    {{ scope.row.status_mes ? '合格' : '不合格' }}
                </template>
            </el-table-column>
            <el-table-column label="PLC判断是否合格" align="center" prop="status_plc">
                <template slot-scope="scope">
                    {{ scope.row.status_plc ? '合格' : '不合格' }}
                </template>
            </el-table-column>

            <el-table-column label="开始时间" align="center" prop="time">

            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="finish" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                        @click="handleUpdate(scope.row)">修改</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.index" :limit.sync="queryParams.size"
            @pagination="getList" />

        <!-- 添加或修改作者对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="680px" :close-on-click-modal="false" append-to-body>
            <!-- <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">添加</el-button> -->
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="PLC是否合格" prop="status_plc" label-width="160">
                    <el-select v-model="form.status_plc" placeholder="请选择">
                        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <!-- <template>
                    <el-table :data="tableData" style="width: 100%">
                        <el-table-column prop="key" label="名称">
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.key"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="value" label="数据">
                            <template slot-scope="scope">
                                <el-input v-model="scope.row.value"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="danger" size="mini" @click="handleDelete(scope.$index)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </template> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import { addtaskList, sampleTask, eidTask } from "@/api/exemplar";

export default {

    data() {
        return {
            dialogVisible: false,
            // 遮罩层

            authorPhoto: {
                key: '',
                value: '',
            },
            options: [{
                value: true,
                label: '合格'
            }, {
                value: false,
                label: '不合格'
            }],
            value: [],
            loading: false,
            tableData: [],
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 作者表格数据
            basicauthorList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                index: 1,
                size: 10,
                start: null,
                finish: null,

            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth() + 1; // 月份从0开始，所以要加1
        var day = today.getDate();
        // 格式化日期
        var formattedDate = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day);

        this.queryParams.start = formattedDate
        this.queryParams.finish = formattedDate
        this.value = [formattedDate, formattedDate]
        this.getList();
    },
    methods: {
        /** 查询作者列表 */
        getList() {
            this.loading = true;
            addtaskList(this.queryParams).then(response => {
                this.basicauthorList = response.data.data
                this.basicauthorList = this.basicauthorList.map(item => {
                    return { ...item, offShow: true };
                });

                this.total = response.data.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.getList();
        },
        // 表单重置
        reset() {
            this.form = {
                sample_id: null,
                status_plc: null
            };
        },

        openTable(row, event) {
            if (event.target.textContent == '展开') {
                this.basicauthorList[row.$index].offShow = false
            } else {
                this.basicauthorList[row.$index].offShow = true
            }

        },

        handleDelete(index) {
            // 点击删除按钮时，删除对应索引的数据项
            this.tableData.splice(index, 1);
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.start = this.value == null ? null : this.value.length > 0 ? this.value[0] : null
            this.queryParams.finish = this.value == null ? null : this.value.length > 0 ? this.value[1] : null


            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            sampleTask().then(res => {
                console.log(res);
            })
            // this.resetForm("queryForm");
            // this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.authorId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.tableData.push({ key: '', value: '' });

        },
        handleExport() {
            this.$confirm('是否将数据导出为EXcel文档？').then(() => {
                const params = new URLSearchParams();
                this.value.length == 0 ? '' : params.append('start', this.value[0]);
                this.value.length == 0 ? '' : params.append('finish', this.value[1]);
                // this.queryParams.facility_id == null ? '' : params.append('sequence', this.queryParams.facility_id);
                const url = new URL('http://192.168.20.252:8080/api/sample/task/output');
                url.search = params.toString();
                window.location.href = url.toString();
            })
        },
        /** 修改按钮操作 */
        handleUpdate(row) {

            this.open = true;
            this.title = "修改样件";
            this.form = row

        },
        /** 提交按钮 */
        submitForm() {
            let newArr = {
                sample_id: this.form.sample_id,
                status_plc: this.form.status_plc,
            }

            this.$refs["form"].validate(valid => {
                if (valid) {
                    eidTask(newArr).then(res => {
                        console.log(res);
                        this.open = false
                        this.getList()
                    })
                }
            });
        },

    }
};
</script>
<style>
.app-container {
    background-color: #fff;
}
</style>
